package com.jiangyao.common.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * web上下文
 *
 * @author: yangyongchuan
 * @create: 2020年05月05日 19:28
 */
@Component
public class WebContextHolder {
	/**
	 * 获取request
	 *
	 * @return request
	 */
	public static HttpServletRequest getRequest() {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return servletRequestAttributes.getRequest();

	}

	/**
	 * 获取response
	 *
	 * @return response
	 */
	public static HttpServletResponse getResponse() {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return servletRequestAttributes.getResponse();
	}

	/**
	 * 获取参数
	 *
	 * @return 结果
	 */
	public static Object getAttribute(String paramName) {
		HttpServletRequest request = getRequest();
		return request.getAttribute(paramName);
	}

}
